package bat.iog.hytool.ftp;

//import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.IOException;

public class FTPListDemo {
    public static void main(String[] args) {
        FTPClient client = new FTPClient();

        try {
            client.connect("127.0.0.1");
            client.login("user", "root");

            if (client.isConnected()) {
                // 获取当前工作文件名列表
                //目录。当找不到文件时，将有一个空数组
                // 被退回。
                String[] names = client.listNames();
                for (String name : names) {
                    System.out.println("Name = " + name);
                }

                FTPFile[] ftpFiles = client.listFiles();
                for (FTPFile ftpFile : ftpFiles) {
                    // 检查FTPFile是否为常规文件
                    if (ftpFile.getType() == FTPFile.FILE_TYPE) {
                        System.out.printf("FTPFile: %s; %s%n",
                                ftpFile.getName()
                                /*FileUtils.byteCountToDisplaySize(ftpFile.getSize())*/);
                    }
                }
            }
            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
